package cz.pproproj.aaservice.guimodule.rest;

import cz.pproproj.aaservice.guimodule.BLLModuleAccess;
import cz.pproproj.aaservice.guimodule.config.AuthService;
import cz.pproproj.aaservice.guimodule.model.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

/**
 * Created by inZa on 28.12.14.
 */
@RestController
class ServiceResource {

   private static final AuthHeaderParser AUTH_HEADER_PARSER = new AuthHeaderParser();
    @Autowired
    private AuthService authService;

    private BLLModuleAccess _bllModuleAccess = new AnnotationConfigApplicationContext("cz.pproproj.aaservice.aasbllmodule").getBean(BLLModuleAccess.class);


    @RequestMapping(method = RequestMethod.GET,
            value="/role/remove/{{role}}")
    public MessageResponse removeRole(@PathVariable String role, @RequestHeader HttpHeaders headers) throws AuthenticationException {
        authService.authenticate(AUTH_HEADER_PARSER.parse(headers),null);
        _bllModuleAccess.removeRole(null);
        return new MessageResponse("Role "+role+" has been removed.");
    }

    @RequestMapping(method = RequestMethod.GET,
            value="/role/add/{{role}}")
    public MessageResponse addRole(@PathVariable String role, @RequestHeader HttpHeaders headers) throws AuthenticationException {
        authService.authenticate(AUTH_HEADER_PARSER.parse(headers),null);
        _bllModuleAccess.addRole(null);
        return new MessageResponse("Role "+role+" has been added.");
    }

    @RequestMapping(method = RequestMethod.POST,
            value="/roles/remove",
            consumes = "application/json",
            produces = "application/json")
    public MessageResponse removeRolesFromUser(@RequestBody Object assignment, @RequestHeader HttpHeaders headers) throws AuthenticationException {
        authService.authenticate(AUTH_HEADER_PARSER.parse(headers),null);
        _bllModuleAccess.removeMapping(null, null);
        return new MessageResponse("Roles has been removed.");
    }

    @RequestMapping(method = RequestMethod.POST,
            value="/roles/add",
            consumes = "application/json",
            produces = "application/json")
    public MessageResponse addRolesFromUser(@RequestBody Object assignment, @RequestHeader HttpHeaders headers) throws AuthenticationException {
        authService.authenticate(AUTH_HEADER_PARSER.parse(headers),null);
        _bllModuleAccess.addMapping(null, null);
        return new MessageResponse("Roles has been added.");
    }

    @RequestMapping(method = RequestMethod.GET,
            value="/roles",
            produces = "application/json")
    public Object getRoles(@RequestHeader HttpHeaders headers) throws AuthenticationException {
        authService.authenticate(AUTH_HEADER_PARSER.parse(headers), null);
        return  _bllModuleAccess.getAllRoles();
    }

    @RequestMapping(method = RequestMethod.GET,
            value="/users",
            produces = "application/json")
    public Object getUsers(@RequestHeader HttpHeaders headers) throws AuthenticationException {
        authService.authenticate(AUTH_HEADER_PARSER.parse(headers),null);
        return  _bllModuleAccess.getAllUsers("db1");
    }



    @RequestMapping(method = RequestMethod.GET,
            value="/roles/{user}",
            produces = "application/json")
    public Object getRoles(@PathVariable String  user, @RequestHeader HttpHeaders headers) throws AuthenticationException {
        authService.authenticate(AUTH_HEADER_PARSER.parse(headers),null);
        return _bllModuleAccess.getRolesFromUser(null);
    }

}
